package com.kczd.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.kczd.client.R;
import com.kczd.fragment.OrderFinishedFragment;
import com.kczd.util.SysApplication;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class OrderFinishedDetailsActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView iv_back;
    private ImageView iv_headimg;
    private ImageView iv_tel;

    private TextView tv_uname;
    private TextView tv_utel;
    private TextView tv_datetime;
    private TextView tv_oriposition;
    private TextView tv_terposition;
    private TextView tv_tol_price;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_finished_details);

        //新页面接收数据
        Bundle bundle = this.getIntent().getExtras();
        //接收name值
        int position = bundle.getInt("position");

        initView();
        initViewData(position);

        // 多activity管理
        SysApplication.getInstance().addActivity(this);
    }

    private void initView() {

        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_headimg = (ImageView) findViewById(R.id.iv_headimg);
        iv_tel = (ImageView) findViewById(R.id.iv_tel);

        tv_uname = (TextView) findViewById(R.id.tv_uname);
        tv_utel = (TextView) findViewById(R.id.tv_utel);
        tv_datetime = (TextView) findViewById(R.id.tv_datetime);
        tv_oriposition = (TextView) findViewById(R.id.tv_oriposition);
        tv_terposition = (TextView) findViewById(R.id.tv_terposition);
        tv_tol_price = (TextView) findViewById(R.id.tv_tol_price);
        iv_back.setOnClickListener(this);
        iv_tel.setOnClickListener(this);
    }

    private void initViewData(int position) {
        tv_uname.setText(OrderFinishedFragment.orderFinishedlsit.get(position).getU_name());
        tv_utel.setText(OrderFinishedFragment.orderFinishedlsit.get(position).getU_tel());
        tv_datetime.setText(OrderFinishedFragment.orderFinishedlsit.get(position).getDatetime());
        tv_oriposition.setText(OrderFinishedFragment.orderFinishedlsit.get(position).getOrigin());
        tv_terposition.setText(OrderFinishedFragment.orderFinishedlsit.get(position).getTerminal());
        tv_tol_price.setText(OrderFinishedFragment.orderFinishedlsit.get(position).getTol_price() + "元");

        String uImage = OrderFinishedFragment.orderFinishedlsit.get(position).getU_img();
        //显示图片的配置
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.img_loading)
                .showImageOnFail(R.mipmap.img_fail)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
        ImageLoader.getInstance().displayImage(uImage, iv_headimg, options);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.iv_back:
                this.finish();
                break;

            case R.id.iv_tel:
                //用intent启动拨打电话
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + tv_utel.getText().toString()));
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                startActivity(intent);
                break;

            default:
                break;
        }
    }
}
